
clear

use "afb_full_r7.dta", clear
keep if strpos(respno, "ZIM") == 1
gen round = "r7"


************************
***   Demographic    ***
************************

gen surveyor_origin = q100
gen surveyor_gov = surveyor_origin == 8 if surveyor_origin != 98 & surveyor_origin != 99
gen surveyor_pol = surveyor_origin == 7 if surveyor_origin != 98 & surveyor_origin != 99

label var surveyor_gov "Government sent surveyor"
label var surveyor_pol "Political party/politician sent surveyor"

* Demographic

gen age = q1 if q1 < 104 & q1 >= 18
gen year_birth = 2017-age

gen schooling = q97 if q97 < 10 & q97 > -1
recode schooling (0/1=0) (2=1) (3=2) (4=3) (5=4) (6/9=5)

gen urban_rural = urbrur
gen urban = urban_rural == 2

gen gender = q101
gen male = gender == 1

gen race = q102
keep if race == 1

gen language = q2a
gen ndebele_language = language == 860 if language != 1 & language != 9995
gen shona_language = (language >= 861 & language <= 868) if language != 1 & language != 9995

gen tribe = q84
gen ndebele = tribe == 860 if tribe < 9990
gen shona = (tribe >= 861 | tribe <= 868) if tribe < 9990

replace ndebele = ndebele_language if ndebele==.
replace ndebele = 0 if ndebele==.
replace ndebele = 1  if ndebele_language==1 & ndebele==0 & shona==0

replace shona = shona_language if shona==.
replace shona = 0 if shona==.
replace shona = 1  if shona_language==1 & shona==0 &  ndebele==0 

gen other = (ndebele ==0 & shona == 0)
drop ndebele_language shona_language

label var age "Age"
label var year_birth "Birth year"
label var schooling "Schooling"
label var urban "Urban"
label var male "Male"
label var language "Language"
label var tribe "Tribe"
label var ndebele "Ndebele tribe"
label var shona "Shona group tribes"
label var other "Other tribes"



******************************
***   Economic variables   ***
******************************

* Economic 

gen no_food = q8a
gen no_water = q8b
gen no_meds = q8c
gen no_fuel = q8d
gen no_cash = q8e

foreach var of varlist no_food-no_cash {
	replace `var' = . if `var' >= 7 | `var' < 0
}

label var no_food "Go without food"
label var no_water "Go without water"
label var no_meds "Go without medicine"
label var no_fuel "Go without fuel"
label var no_cash "No cash"


* Assets

gen owns_radio = q89a
gen owns_tv = q89b
gen owns_vehicle = q89c
gen owns_mobile = q89f

foreach var of varlist owns_radio-owns_mobile {
	recode `var' (2=1) (1=0)
	replace `var' = . if `var' >= 8 | `var' < 0
}

label var owns_radio "Owns radio"
label var owns_tv "Owns TV"
label var owns_vehicle "Owns motor vehicle"
label var owns_mobile "Owns mobile phone"


* Employment

gen empl_stat = q94
gen look_for_job = (empl_stat == 1) if empl_stat < 9 & empl_stat >= 0
gen part_time = empl_stat == 2 if empl_stat < 9 & empl_stat >= 0
gen full_time = empl_stat == 3 if empl_stat < 9 & empl_stat >= 0

label var look_for_job "No job and looking for job"
label var part_time "Part time job"
label var full_time "Full time job"


*****************************************
***   Political Engagement Variables  ***
*****************************************

* Local engagement

gen assoc_rel = q20a
gen assoc_comm = q20b
gen raise_issue = q21b

foreach var of varlist assoc_rel-raise_issue {
	replace `var' = . if `var' >= 8 | `var' == -1
}

label var assoc_rel "Member of religious association"
label var assoc_comm "Member of community association"
label var raise_issue "Join others in raising issues"


* Political engagement

gen att_rally = q24a
gen att_pol_meeting = .
gen persuade_others = .
gen work_for_cand = q24b
gen int_pub_aff = .
gen disc_pol = q13

foreach var of varlist att_rally - disc_pol {
	tab `var'
	replace `var' = . if `var' >= 4 | `var' == -1
}

label var att_rally "Attend campaign rally"
label var att_pol_meeting "Attend campaign meeting"
label var persuade_others "Persuade others to vote for party"
label var work_for_cand "Worked for candidate"
label var int_pub_aff "Interested in public affairs"
label var disc_pol "Discusses politics"

gen req_action = q26a
gen contact_med = q26b
gen contact_official = q26c
gen refuse_tax = q26d
gen att_demo = q26e

foreach var of varlist req_action-att_demo {
	tab `var'
	replace `var' = . if `var' >= 5 | `var' == -1
}

label var req_action "Join others to request gov action"
label var contact_med "Contact media for change"
label var contact_official "Contact officials for change"
label var refuse_tax "Refuse to pay tax or fee to gov"
label var att_demo "Attend demonstration"


* Consuming political news

gen radio = q12a
gen tv = q12b
gen newspapers = q12c
gen internet = q12d
gen social_media = q12e

foreach var of varlist radio-social_media {
	tab `var'
	replace `var' = . if `var' >= 5 | `var' == -1
}

label var radio "Listens to radio for news"
label var tv "Watches TV for news"
label var newspapers "Reads newspaper for news"
label var internet "Uses internet for news"
label var social_media "Uses social media for news"


* Voting

gen registered = q22 >=1
replace registered = . if q22 >= 8

gen voted = q22 == 1
replace voted = . if q22 >= 8

label var registered "Registered to vote"
label var voted "Voted in elections"


****************************************************
***   Understanding of democratic institutions   ***
****************************************************


* Democracy

gen elec_free = q23-1 if q23 < 8

gen hold_accountable = q29
gen pro_elections = q30
gen parties_needed = q31
gen opp_hold_acc = .
gen media_hold_acc = .
gen parl_laws = q32
gen pres_obey_laws = q33
gen two_term = q34

foreach var of varlist hold_accountable-two_term {
	recode `var' (1=0) (2=1) (5=2) (9/98=.) (-1=.)
	tab1 `var'
}

foreach var of varlist pro_elections parl_laws two_term {
	recode `var' (4=0) (3=1) (2=2) (1=3) (0=4)
}

label var elec_free "Freeness and fairness of last elections"

label var hold_accountable "Gov produce results quickly vs. citizens hold gov accountable"
label var pro_elections "Don't elect vs. elect officials"
label var parties_needed "Parties generate chaos vs. parties necessary"
label var opp_hold_acc "Opposition work with gov vs. opposition act as watchdog"
label var media_hold_acc "Media avoid negative reporting vs. media checks gov"
label var parl_laws "Pres makes law vs. parl makes laws"
label var pres_obey_laws "Pres doesn't need to obey laws vs. pres must obey laws"
label var two_term "No term limit vs. two term limit"


********************************
***   Quality of democracy   ***
********************************

gen democracy = q35-1 if q35 < 7
gen sat_demo = q36 if q36 < 7

gen media_abuse_freedom = .
gen party_comp_confl = q39a

gen parl_accountable = .
gen remove_leaders = .

gen careful_say = q42a

gen votes_fair = q23-1
gen bribed = .
gen real_choice = .
gen threatened = .
gen fear_intimidation = q40
recode fear_intimidation (0=3) (1=2) (2=1) (3=0)

gen freedom_say = q19a-1
gen freedom_join = q19b-1
gen freedom_choose = .


* Authoritarian preferences

gen reject_one_party = q27a-1
gen reject_military = q27b-1
gen reject_one_man = q27c-1

foreach var of varlist elec_free-reject_one_man {
	replace `var' = . if `var' >= 7 | `var' <= -1
	tab `var'
}

foreach var of varlist reject_* {
	recode `var' (0=4) (1=3) (2=2) (3=1) (4=0)
}

label var democracy "Zimbabwe is a democracy"
label var sat_demo "Satisfied with democracy in Zimbabwe"
label var media_abuse_freedom "Media abuses its freedoms"
label var party_comp_confl "Party competition leads to conflict"

label var parl_accountable "Elections ensures citizens' views are reflected in parl"
label var remove_leaders "Elections allows citizens to remove bad leaders"

label var reject_one_party "Reject single party rule"
label var reject_military "Reject military rule"
label var reject_one_man "Reject one man rule"

label var freedom_say "Free to say what you think"
label var freedom_join "Free to join any political organization"
label var freedom_choose "Free to choose who to vote for"


* Views about government

gen trust_pres = q43a
gen trust_mp = q43b 
gen trust_ec = q43c
gen trust_tax = .
gen trust_lg = q43d
gen trust_inc = q43e
gen trust_opp = q43f
gen trust_police = q43g
gen trust_army = q43h
gen trust_courts = q43i
gen trust_trad = q43j
gen trust_relig = q43k

foreach var of varlist trust_pres-trust_relig {
	replace `var' = . if `var' >= 6 | `var' < 0
	tab `var'
}

label var trust_pres "Trust president"
label var trust_mp "Trust MP"
label var trust_ec "Trust Electoral Commission"
label var trust_tax "Trust tax office"
label var trust_lg "Trust LG"
label var trust_inc "Trust incumbent party"
label var trust_opp "Trust opposition party"
label var trust_police "Trust police"
label var trust_army "Trust army"
label var trust_courts "Trust courts and dept of justice"
label var trust_trad "Trust traditional leaders"
label var trust_relig "Trust religious leaders"


* Public goods

gen post_office = ea_fac_a == 1 if ea_fac_a != -1 & ea_fac_a <= 2
gen school = ea_fac_b == 1 if ea_fac_b != -1 & ea_fac_b <= 2
gen police_station = ea_fac_c == 1 if ea_fac_c != -1 & ea_fac_c <= 2
gen transport = ea_fac_g == 1 if ea_fac_g != -1 & ea_fac_g <= 2
gen electricity = ea_svc_a == 1 if ea_svc_a != -1 & ea_svc_a <= 2
gen piped_water = ea_svc_b == 1 if ea_svc_b != -1 & ea_svc_b <= 2
gen sewage = ea_svc_c == 1 if ea_svc_c != -1 & ea_svc_c <= 2
gen cell_service = ea_svc_d == 1 if ea_svc_d != -1 & ea_svc_d <= 2
gen health_clinic = ea_fac_d == 1 if ea_fac_d != -1 & ea_fac_d <= 2
gen rec_facilities = .
gen placesof_worship = .
gen townhalls = .
gen petrol_station = .
gen bottle_store = .
gen supermarket = .
gen cafes = .
gen market_stalls = ea_fac_e == 1 if ea_fac_e != -1 & ea_fac_e <= 2
gen bank = ea_fac_f == 1 if ea_fac_f != -1 & ea_fac_f <= 2
gen police_vehicles = ea_sec_a == 1 if ea_sec_a != -1 & ea_sec_a <= 2
gen soldiers_army = ea_sec_b == 1 if ea_sec_b != -1 & ea_sec_b <= 2
gen militia = .
gen roadblack_police = ea_sec_c == 1 if ea_sec_c != -1 & ea_sec_c <= 2
gen roadblock_private = .
gen customs_checkpoint = ea_sec_d == 1 if ea_sec_d != -1 & ea_sec_d <= 2
gen roadblock_community = ea_sec_e == 1 if ea_sec_e != -1 & ea_sec_e <= 2

gen pavement = ea_road_a >= 5 if ea_road_a != -1 


*************************
***   Final cleanup   ***
*************************

keep round respno dateintr strtime surveyor_gov - pavement

drop tribe language

foreach var of varlist surveyor_gov - pavement {
	disp "`var'"
	count if (`var' < -1 | `var' >= 6) & `var' != .
}

split dateintr, parse("/")
ren dateintr3 year
ren dateintr2 day
ren dateintr1 month

destring year day month, replace
drop dateintr*
